﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;

namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OCompleteMacNGReportPageHighcharts : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "OCompleteMacNGReport";
            }
        }

        protected int Year
        {
            get
            {
                return GetRequestValue<int>("year", DateTime.Now.Year);
            }
        }
        protected int Month
        {
            get
            {
                return GetRequestValue<int>("Month", DateTime.Now.Month);
            }
        }
        /// <summary>
        /// 当前要显示的问题分析月份 格式为2011-07
        /// </summary>
        protected string CurrShowMonth
        {
            get
            {
                return Year.ToString() + "-" + Month.ToString();
            }
        }

        /// <summary>
        /// 报表要开始显示的月份,上半年还是下半年
        /// </summary>
        protected int StateMonth
        {
            get
            {
                if (Month <= 6)
                    return 1;
                else
                    return 7;
            }
        }
        /// <summary>
        /// 报表要结速显示的月份,上半年还是下半年
        /// </summary>
        protected int EndMonth
        {
            get
            {
                return StateMonth + 5;
            }
        }



        protected string GetYearList()
        {
            string liTemplete = "<option value='{0}' {1}>{0}年</option>";
            string rHtml = "";
            int nowYear = DateTime.Now.Year;
            for (int i = (nowYear - 3); i <= nowYear; i++)
            {
                rHtml = rHtml + string.Format(liTemplete, i, i == Year ? "selected" : "");
            }
            return rHtml;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            rpreport4container.DataSource = report5dts;
            rpreport4container.DataBind();

            rpreport6container.DataSource = report6dts;
            rpreport6container.DataBind();
        }
        #region 相关方法
        /// <summary>
        /// 获取报表1相关数据
        /// </summary>
        /// <param name="tYear"></param>
        /// <param name="tMoneth"></param>
        /// <returns>
        /// 只返回一行
        /// 列AllCount表示所有的性能检测台数
        /// 列NGCount表示有出现问题记录的台数
        /// 列NotNGCount表示未出现问题的台数
        /// 列FPY表示合格率(NotNGCount/AllCount)
        /// </returns>
        protected DataRow OInspection_GetReport1(object tYear, object tMoneth)
        {
            DateTime statedate = Convert.ToDateTime(tYear.ToString() + "-" + tMoneth.ToString() + "-01 00:00:00");
            DateTime enddate = Convert.ToDateTime(statedate.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));

            return OInspectionController.OInspection_GetReport1(statedate, enddate).Rows[0];
        }
        /// <summary>
        /// 获取报表2相关数据
        /// </summary>
        /// <param name="tYear"></param>
        /// <param name="tMoneth"></param>
        /// <returns>
        /// 只返回一行
        /// 列AllCount表示所有的出入库检查台数
        /// 列NGCount表示有出现问题记录的台数
        /// 列NotNGCount表示未出现问题的台数
        /// 列FPY表示合格率(NotNGCount/AllCount)
        /// </returns>
        protected DataRow OInspection_GetReport2(object tYear, object tMoneth)
        {
            DateTime statedate = Convert.ToDateTime(tYear.ToString() + "-" + tMoneth.ToString() + "-01 00:00:00");
            DateTime enddate = Convert.ToDateTime(statedate.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));

            return OInspectionController.OInspection_GetReport2(statedate, enddate).Rows[0];
        }
        /// <summary>
        /// 获取指定时间段内机型
        /// </summary>
        /// <param name="tYear"></param>
        /// <param name="tMoneth"></param>
        /// <returns></returns>
        protected List<string> GetReportMacType3(object tYear, object tMoneth)
        {
            DateTime statedate = Convert.ToDateTime(tYear.ToString() + "-" + tMoneth.ToString() + "-01 00:00:00");
            DateTime enddate = Convert.ToDateTime(statedate.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));
            DataTable DT = OInspectionController.OInspection_GetReportMacType3(statedate, enddate);
            List<string> lisCol = new List<string>();
            foreach (DataRow dr in DT.Rows)
            {
                lisCol.Add(dr["MachineType"].ToString());
            }
            if (lisCol.Count <= 0)
            {
                lisCol.Add("55NSL");
                lisCol.Add("65NS");
                lisCol.Add("80NS");
            }
            return lisCol;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ItemType"></param>
        /// <param name="colLabel">colLabel格式为 2011-07,55NSL</param>
        /// <returns></returns>
        protected object OInspection_GetReport3(string ItemType, string colLabel)
        {
            string[] cols = colLabel.Split(new char[] { ',' });
            DateTime statedate = Convert.ToDateTime(cols[0] + "-01 00:00:00");
            DateTime enddate = Convert.ToDateTime(statedate.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));
            string macType = cols[1];
            return OInspectionController.OInspection_GetReport3(macType, ItemType, statedate, enddate);

        }

        protected DataTable OInspection_GetReport4(object tYear, object tMoneth)
        {
            DateTime statedate = Convert.ToDateTime(tYear.ToString() + "-" + tMoneth.ToString() + "-01 00:00:00");
            DateTime enddate = Convert.ToDateTime(statedate.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));

            return OInspectionController.OInspection_GetReport4(statedate, enddate);
        }

        protected DataTable OInspection_GetReport5(object tYear, object tMoneth)
        {
            DateTime statedate = Convert.ToDateTime(tYear.ToString() + "-" + tMoneth.ToString() + "-01 00:00:00");
            DateTime enddate = Convert.ToDateTime(statedate.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));

            return OInspectionController.OInspection_GetReport5(statedate, enddate);
        }

        protected DataTable OInspection_GetReport6(object tYear, object tMoneth)
        {
            DateTime statedate = Convert.ToDateTime(tYear.ToString() + "-" + tMoneth.ToString() + "-01 00:00:00");
            DateTime enddate = Convert.ToDateTime(statedate.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));

            return OInspectionController.OInspection_GetReport6(statedate, enddate);
        }
        #endregion

        #region 1.一次性能检测合格台数


        private DataTable _report1dts = null;
        private DataTable report1dts
        {
            get
            {
                if (_report1dts == null)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("月份", typeof(string));
                    dt.Columns.Add("百分比", typeof(decimal));
                    for (int sindex = StateMonth; sindex <= EndMonth; sindex++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["月份"] = string.Format("{0}月", sindex);
                        dr["百分比"] = Convert.ToDecimal(Convert.ToDecimal(OInspection_GetReport1(Year, sindex)["FPY"]).ToString("F1"));
                        //dt.Columns.Add(string.Format("{0}年{1}月", Year, sindex), typeof(string));
                        dt.Rows.Add(dr);
                    }

                    _report1dts = dt;
                }
                return _report1dts;
            }
        }

        protected string GetReport1JSCategories()
        {
            DataTable dtcl = report1dts;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int rowindex = 1;
            foreach (DataRow dr in dtcl.Rows)
            {
                sb.AppendFormat("'{0}'", dr[0]);
                if (rowindex != dtcl.Rows.Count)
                {
                    sb.AppendFormat(",");
                }
                rowindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport1JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = report1dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");

            string str直接 = "{name: '一次性能检测',data:[";
            int rowindex = 1;
            foreach (DataRow dr in dtmain.Rows)
            {
                str直接 += Convert.ToDecimal(dr["百分比"]).ToString("f2");
                
                if (rowindex != dtmain.Rows.Count)
                {
                    str直接 += ",";
                }
                rowindex++;
            }
            str直接 += "]}";

            sb.AppendLine(str直接);
            sb.AppendLine("]");
            return sb.ToString();
        }
        #endregion

        #region 2.一次出入库检查合格台数


        private DataTable _report2dts = null;
        private DataTable report2dts
        {
            get
            {
                if (_report2dts == null)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("月份", typeof(string));
                    dt.Columns.Add("百分比", typeof(decimal));
                    for (int sindex = StateMonth; sindex <= EndMonth; sindex++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["月份"] = string.Format("{0}月", sindex);
                        dr["百分比"] = Convert.ToDecimal(Convert.ToDecimal(OInspection_GetReport2(Year, sindex)["FPY"]).ToString("F1"));
                        //dt.Columns.Add(string.Format("{0}年{1}月", Year, sindex), typeof(string));
                        dt.Rows.Add(dr);
                    }

                    _report2dts = dt;
                }
                return _report2dts;
            }
        }

        protected string GetReport2JSCategories()
        {
            DataTable dtcl = report2dts;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int rowindex = 1;
            foreach (DataRow dr in dtcl.Rows)
            {
                sb.AppendFormat("'{0}'", dr[0]);
                if (rowindex != dtcl.Rows.Count)
                {
                    sb.AppendFormat(",");
                }
                rowindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport2JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = report2dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");

            string str直接 = "{name: '一次出入库检查',data:[";
            int rowindex = 1;
            foreach (DataRow dr in dtmain.Rows)
            {
                str直接 += Convert.ToDecimal(dr["百分比"]).ToString("f2");

                if (rowindex != dtmain.Rows.Count)
                {
                    str直接 += ",";
                }
                rowindex++;
            }
            str直接 += "]}";

            sb.AppendLine(str直接);
            sb.AppendLine("]");
            return sb.ToString();
        }
        #endregion

        #region 3.各机型不良率 1 性能测试


        private DataTable _report3_1dts = null;
        private DataTable report3_1dts
        {
            get
            {
                if (_report3_1dts == null)
                {
                    string itemType = "性能检测不良率";
                    DataTable dt = new DataTable();
                    dt.Columns.Add("月份", typeof(string));

                    List<string> colNames = new List<string>();
                    for (int sindex = StateMonth; sindex <= EndMonth; sindex++)
                    {
                        List<string> colNames_temp = GetReportMacType3(Year, sindex);

                        foreach (string itm in colNames_temp)
                        {
                            if (!colNames.Contains(itm))
                            {
                                colNames.Add(itm);
                            }
                        }
                    }
                    foreach (string itm in colNames)
                    {
                        dt.Columns.Add(itm, typeof(decimal));
                    }

                    for (int sindex = StateMonth; sindex <= EndMonth; sindex++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["月份"] = string.Format("{0}月", sindex);
                        foreach (string itm in colNames)
                        {
                            dr[itm] = Convert.ToDecimal(Convert.ToDecimal(OInspection_GetReport3(itemType, Year.ToString() + "-" + sindex.ToString() + "," + itm)).ToString("f1"));
                            // dt.Columns.Add(itm, typeof(decimal));
                        }
                        dt.Rows.Add(dr);
                    }

                    _report3_1dts = dt;
                }
                return _report3_1dts;
            }
        }

        protected string GetReport3_1JSCategories()
        {
            DataTable dtcl = report3_1dts;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int rowindex = 1;
            foreach (DataRow dr in dtcl.Rows)
            {
                sb.AppendFormat("'{0}'", dr[0]);
                if (rowindex != dtcl.Rows.Count)
                {
                    sb.AppendFormat(",");
                }
                rowindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport3_1JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = report3_1dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");
            int cNum = 1;
            foreach (DataColumn dc in dtmain.Columns)
            {

                if (dc.ColumnName == "月份")
                    continue;

                string str直接 = "{name: '" + dc.ColumnName + "',data:[";
                int rowindex = 1;
                foreach (DataRow dr in dtmain.Rows)
                {
                    str直接 += Convert.ToDecimal(dr[dc.ColumnName]).ToString("f2");

                    if (rowindex != dtmain.Rows.Count)
                    {
                        str直接 += ",";
                    }
                    rowindex++;
                }
                str直接 += "]}";

                if (cNum != dtmain.Columns.Count-1)
                {
                    str直接 += ",";
                }
                sb.AppendLine(str直接);

                cNum++;
            }
            sb.AppendLine("]");
            return sb.ToString();
        }
        #endregion

        #region 3.各机型不良率 2 出入库


        private DataTable _report3_2dts = null;
        private DataTable report3_2dts
        {
            get
            {
                if (_report3_2dts == null)
                {
                    string itemType = "出入库不良率";
                    DataTable dt = new DataTable();
                    dt.Columns.Add("月份", typeof(string));

                    List<string> colNames = new List<string>();
                    for (int sindex = StateMonth; sindex <= EndMonth; sindex++)
                    {
                        List<string> colNames_temp = GetReportMacType3(Year, sindex);

                        foreach (string itm in colNames_temp)
                        {
                            if (!colNames.Contains(itm))
                            {
                                colNames.Add(itm);
                            }
                        }
                    }
                    foreach (string itm in colNames)
                    {
                        dt.Columns.Add(itm, typeof(decimal));
                    }

                    for (int sindex = StateMonth; sindex <= EndMonth; sindex++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["月份"] = string.Format("{0}月", sindex);
                        foreach (string itm in colNames)
                        {
                            dr[itm] = Convert.ToDecimal(Convert.ToDecimal(OInspection_GetReport3(itemType, Year.ToString() + "-" + sindex.ToString() + "," + itm)).ToString("f1"));
                            // dt.Columns.Add(itm, typeof(decimal));
                        }
                        dt.Rows.Add(dr);
                    }

                    _report3_2dts = dt;
                }
                return _report3_2dts;
            }
        }

        protected string GetReport3_2JSCategories()
        {
            DataTable dtcl = report3_2dts;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int rowindex = 1;
            foreach (DataRow dr in dtcl.Rows)
            {
                sb.AppendFormat("'{0}'", dr[0]);
                if (rowindex != dtcl.Rows.Count)
                {
                    sb.AppendFormat(",");
                }
                rowindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport3_2JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = report3_2dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");
            int cNum = 1;
            foreach (DataColumn dc in dtmain.Columns)
            {

                if (dc.ColumnName == "月份")
                    continue;

                string str直接 = "{name: '" + dc.ColumnName + "',data:[";
                int rowindex = 1;
                foreach (DataRow dr in dtmain.Rows)
                {
                    str直接 += Convert.ToDecimal(dr[dc.ColumnName]).ToString("f2");

                    if (rowindex != dtmain.Rows.Count)
                    {
                        str直接 += ",";
                    }
                    rowindex++;
                }
                str直接 += "]}";

                if (cNum != dtmain.Columns.Count - 1)
                {
                    str直接 += ",";
                }
                sb.AppendLine(str直接);

                cNum++;
            }
            sb.AppendLine("]");
            return sb.ToString();
        }
        #endregion

        #region 4.生产口质信息表


        private DataTable _report4dts = null;
        private DataTable report4dts
        {
            get
            {
                if (_report4dts == null)
                {
                    _report4dts = OInspection_GetReport4(Year, Month);
                }
                return _report4dts;
            }
        }

        protected string GetReport4JSCategories()
        {
            DataTable dtcl = report4dts;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int rowindex = 1;
            foreach (DataRow dr in dtcl.Rows)
            {
                sb.AppendFormat("'{0}'", dr[0]);
                if (rowindex != dtcl.Rows.Count)
                {
                    sb.AppendFormat(",");
                }
                rowindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport4JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = report4dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");

            string str直接 = "{name: '工位',data:[";
            int rowindex = 1;
            foreach (DataRow dr in dtmain.Rows)
            {
                str直接 += Convert.ToDecimal(dr["ngcount"]).ToString("f0");

                if (rowindex != dtmain.Rows.Count)
                {
                    str直接 += ",";
                }
                rowindex++;
            }
            str直接 += "]}";

            sb.AppendLine(str直接);
            sb.AppendLine("]");
            return sb.ToString();
        }

        protected string GetShowDataTable()
        {
            string dthtml = "";
            DataTable dt = report4dts;
            dthtml = "<table class='table'><tr>{0}</tr><tr>{1}</tr></table>";
            string StationHtml = "<td>工位</td>";
            string ngcountHtml = "<td>NG数</td>";
            foreach (DataRow dr in dt.Rows)
            {
                StationHtml += string.Format("<td>{0}</td>", dr["Station"]);
                ngcountHtml += string.Format("<td>{0}</td>", dr["ngcount"]);
            }
            return string.Format(dthtml, StationHtml, ngcountHtml);
        }
        #endregion

        #region 四.重点问题汇总


        private DataTable _report5dts = null;
        private DataTable report5dts
        {
            get
            {
                if (_report5dts == null)
                {
                    _report5dts = OInspection_GetReport5(Year, Month);
                }
                return _report5dts;
            }
        }

        protected string GetReport5JSSeriesData()
        {
            /*
                [
				        ['Firefox', 45.0],
				        ['IE', 26.8],
                        ['Chrome',12.8],
				        ['Safari', 8.5],
				        ['Opera', 6.2],
				        ['Others', 0.7]
			        ]
             */
            DataTable dtmain = report5dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");
            string str直接 = "";
            int rowindex = 1;
            foreach (DataRow dr in dtmain.Rows)
            {
                str直接 += string.Format("['{0}', {1}]",dr["Exceptionrecord"],dr["NGCount"]); //Convert.ToDecimal(dr["ngcount"]).ToString("f0");

                if (rowindex != dtmain.Rows.Count)
                {
                    str直接 += ",";
                }
                rowindex++;
            }

            sb.AppendLine(str直接);
            sb.AppendLine("]");
            return sb.ToString();
        }
        #endregion

        #region 五.外协件(性能测试与出入库检查)品异汇总


        private DataTable _report6dts = null;
        private DataTable report6dts
        {
            get
            {
                if (_report6dts == null)
                {
                    _report6dts = OInspection_GetReport6(Year, Month);
                }
                return _report6dts;
            }
        }

        protected string GetReport6JSCategories()
        {
            DataTable dtcl = report6dts;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int rowindex = 1;
            foreach (DataRow dr in dtcl.Rows)
            {
                sb.AppendFormat("'{0}'", dr[0]);
                if (rowindex != dtcl.Rows.Count)
                {
                    sb.AppendFormat(",");
                }
                rowindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport6JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = report6dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");

            string str直接 = "{name: '供应商',data:[";
            int rowindex = 1;
            foreach (DataRow dr in dtmain.Rows)
            {
                str直接 += Convert.ToDecimal(dr["NGCount"]).ToString("f0");

                if (rowindex != dtmain.Rows.Count)
                {
                    str直接 += ",";
                }
                rowindex++;
            }
            str直接 += "]}";

            sb.AppendLine(str直接);
            sb.AppendLine("]");
            return sb.ToString();
        }
        #endregion
    }
}